# -*- coding: utf-8 -*-
# time: 2025/4/2 14:50
# file: ch01.py
# author: hanson
import pdfplumber
from PyPDF2 import PdfReader
from langchain_community.document_loaders import PyPDFLoader

Load_path = r"F:\workspace\py_project\intellect\llm\pdf"

# 初始化加载器
file_path = "example.pdf"
loader = PyPDFLoader(Load_path+"\LLM_Survey_Chinese.pdf")

# 加载全部内容（分页存储）
docs = loader.load()  # ‌:ml-citation{ref="1,2" data="citationList"}
print(f"总页数: {len(docs)}")
print(f"第一页内容: {docs[0].page_content}")
print(f"元数据（页码）: {docs[0].metadata}")

# 分块处理长文本
from langchain_text_splitters import RecursiveCharacterTextSplitter

# 分割文本（按每 1000 字符分块）
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
chunks = text_splitter.split_documents(docs)  # ml-citation{ref="1,3" data="citationList"}
print(f"分块数量: {len(chunks)}")
# 3. 输出结果
print(f"分块后文本示例：{chunks[0].page_content}") # 需要使用for 循环遍历所有分块

"""
发现 PyPDFLoader 也是解析pdf 乱码

"""